package com.hzlh.service.impl;

import com.hzlh.dao.TCompanyBaseDao;
import com.hzlh.model.TCompanyBase;
import com.hzlh.model.TCompanyBaseFull;
import com.hzlh.model.TCompanyBaseVo;
import com.hzlh.service.ICompanyBaseService;
import com.hzlh.utils.GetDistanceUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CompanyBaseServiceImpl implements ICompanyBaseService {
    @Resource
    TCompanyBaseDao tCompanyBaseDao;

    @Override
    public List<TCompanyBaseVo> selectList(Map<String, Object> param) {
        param = CollectionUtils.isEmpty(param) ? new HashMap<>() : param;
        return tCompanyBaseDao.selectList(param);
    }

    @Override
    public TCompanyBase selectInfo(String companyName) {
        return tCompanyBaseDao.selectInfo(companyName);
    }

    @Override
    public int updateArea(TCompanyBaseVo tCompanyBaseVo) {
        return tCompanyBaseDao.updateArea(tCompanyBaseVo);
    }

    @Override
    public String selectRelList(Map<String, Object> param) {
        param = CollectionUtils.isEmpty(param) ? new HashMap<>() : param;
        List<TCompanyBaseVo> list = tCompanyBaseDao.selectRelList(param);
        TCompanyBaseVo companyBaseVo = null;
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                companyBaseVo = list.get(i);
                if (StringUtils.isNotEmpty(companyBaseVo.getCompanyRel())) {
                    return companyBaseVo.getCompanyRel();
                }
            }
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> selectAreaCount() {
        return tCompanyBaseDao.selectAreaCount();
    }

    @Override
    public List<Map<String, Object>> selectZoneCount() {
        return tCompanyBaseDao.selectZoneCount();
    }

    @Override
    public List<Map<String, Object>> selectCompanyCount() {
        return tCompanyBaseDao.selectCompanyCount();
    }

    @Override
    public TCompanyBaseFull selectFull(String companyName) {
        TCompanyBaseFull tCompanyBaseFull = tCompanyBaseDao.selectFull(companyName);
        return tCompanyBaseFull;
    }

    @Override
    public List<TCompanyBaseVo> drawCircle(BigDecimal lon, BigDecimal lat, BigDecimal radius) {
        Map<String, Double> map = GetDistanceUtils.GetRectRange(lat.doubleValue(), lon.doubleValue(), radius.doubleValue());
        List<TCompanyBaseVo> tCompanyBaseVos = tCompanyBaseDao.selectRectRange(map);
        List<TCompanyBaseVo> companyBaseVos = tCompanyBaseVos.stream().filter(v ->
                GetDistanceUtils.GetDistance(v.getLocLon().doubleValue(), v.getLocLat().doubleValue(), lon.doubleValue(), lat.doubleValue()) <= radius.doubleValue())
                .collect(Collectors.toList());
        return companyBaseVos;
    }
}
